package com.yx.cloud.network.ble;

import android.os.Handler;
import android.os.HandlerThread;

public class DelayTask {
    private Runnable runnable;
    private long timeout;
    private HandlerThread handlerThread;
    private volatile Handler handler;

    public DelayTask(Runnable runnable, long timeout) {
        this.runnable = runnable;
        this.timeout = timeout;
        handlerThread = new HandlerThread("Cancelable thread");
        handlerThread.start();
        handler = new Handler(handlerThread.getLooper());
        handler.postDelayed(runnable, timeout);
    }

    public void cancel() {
        handler.removeCallbacks(runnable);

    }

    public interface CancelCallback {
        void onCanceled();
    }
}
